package com.wl.dzt.sys.controller.action;

import com.wl.dzt.sys.controller.param.Param;
import com.wl.dzt.sys.controller.result.ActionResult;
import com.wl.dzt.sys.core.dao.DB;
import com.wl.dzt.sys.core.dao.datachange.DataChangeter;
import com.wl.dzt.sys.core.dao.select.Select;
import com.wl.dzt.sys.core.dao.tree.TreeBuilder;
import com.wl.dzt.sys.core.dao.tree.checked.AllCheckedModel;
import com.wl.dzt.sys.core.dao.tree.checked.ChildrenCheckedModel;
import com.wl.dzt.sys.core.dao.tree.checked.ParentCheckedModel;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 树形动作
 * 用于执行树形请求
 * 根据参数执行树形请求
 */
@Component
public class TreeAction implements Action {

    /**
     * 树形选择模式
     */
    public static Map<String, DataChangeter> CK_MODELS = new HashMap<>();

    /**
     * <b>填充树形选取模式</b>
     * <p>
     * 全部都带选择框<br>
     * 子节点带选择框<br>
     * 父节点带选择框<br>
     * </p>
     */
    @PostConstruct
    public void init() {
        CK_MODELS.put("all", new AllCheckedModel());
        CK_MODELS.put("children", new ChildrenCheckedModel());
        CK_MODELS.put("parent", new ParentCheckedModel());
    }

    public ActionResult doAction(Param param) {
        ActionResult actionResult = new ActionResult();
        Select select = param.toSelect();
        List<Map<String, Object>> data = TreeBuilder.cascadeTree(DB.list(select), param.getTreeSetting());
        if (param.getDataChangeter() != null) {
            param.getDataChangeter().change(data);
        }
        DataChangeter changeter = CK_MODELS.get(param.getTreeCheckedModel());
        if (changeter != null) {
            changeter.change(data);
        }
        actionResult.setTreeData(data);
        return actionResult;
    }
}
